package com.qdsg.ysg.doctor.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.shape.view.ShapeTextView;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.base.BaseApplication;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UIConstants;
import com.rest.business.RestProxy;
import com.rest.response.IMListResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.UtilList;

/**
 * Created by HP on 2020/2/20.
 */

public class HistoryListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.cl_empty)
    ConstraintLayout clEmpty;

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    MyAdapter adapter;

    List<IMListResponse.IMItem> list = new ArrayList<>();


    @Override
    protected int getLayoutResource() {
        return R.layout.aty_base_list;
    }

    @Override
    protected void initViews() {
        tvTitle.setText(R.string.string_historical_consultation);
        LinearLayoutManager manager = new LinearLayoutManager(HistoryListActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> initData());
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {

        });
    }

    @Override
    protected void initData() {
        queryIMList();
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @NotNull
        @Override
        public Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(HistoryListActivity.this).inflate(R.layout.item_cloud, parent, false));
            return myHolder;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(v -> {
                Bundle bundle = new Bundle();
                bundle.putString(UIConstants.DIAGNOSE_ID, list.get(position).diagnoseId);
                startActivity(ChatActivity.class, bundle);
            });

            String flag = list.get(position).flag;
            int diagType = list.get(position).diagType;
            switch (flag) {
                case "0":
                    holder.tv_status.setText(R.string.string_not_diagnosed);
                    break;
                case "1":
                    holder.tv_status.setText(R.string.string_In_diagnostics);
                    break;
                case "2":
                    holder.tv_status.setText(R.string.string_ended);
                    break;
                default:
                    break;

            }

            holder.tv_name.setText(list.get(position).patientName);
            holder.tv_detail.setText(list.get(position).sex + "  " + list.get(position).age + "岁");
            holder.tv_time.setText(getString(R.string.string_appointment_time) + list.get(position).createTime);

            String yibao = list.get(position).yibao;
            if ("0".equals(yibao)) {
                holder.iv_yibao.setImageResource(R.mipmap.icon_no_yibao);
            } else {
                holder.iv_yibao.setImageResource(R.mipmap.icon_yibao);
            }
            if (list.get(position).businessType == UIConstants.GRAPHIC_TYPE_1) {
                holder.tv_consult_type.setText(R.string.string_consult);
//                holder.tv_consult_type.setBackgroundColor(getResources().getColor(R.color.color_grey_button_normal));

            } else if (list.get(position).businessType == UIConstants.GRAPHIC_TYPE_2) {
                holder.tv_consult_type.setText(R.string.string_referral);
//                holder.tv_consult_type.setBackgroundColor(getResources().getColor(R.color.chooser_orange));
            }

            //ImageLoaderHelper.getInstance().GlideImageLoader( HistoryListActivity.this, list.get(position).picture, holder.img_head, R.mipmap.img_default);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_detail, tv_status, tv_time;
            ShapeTextView tv_consult_type;
            ImageView iv_yibao;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_time = itemView.findViewById(R.id.tv_time);
                tv_detail = itemView.findViewById(R.id.tv_detail);
                tv_status = itemView.findViewById(R.id.tv_status);
                iv_yibao = itemView.findViewById(R.id.iv_yibao);
                tv_consult_type = itemView.findViewById(R.id.tv_consult_type);
            }
        }
    }


    private void queryIMList() {
        RestProxy.getInstance().queryHistoryIMList(BaseApplication.currentUserId, new Observer<IMListResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(IMListResponse imListResponse) {
                try {
                    if (!UtilList.isEmpty(imListResponse.data.records)) {
                        list.clear();
                        list.addAll(imListResponse.data.records);
                        adapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    // 捕获异常，如果要捕获Json数据异常，写catch(JSONException e)
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(HistoryListActivity.this, e);
                refreshLayout.finishRefresh(false);
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh(true);
            }
        });
    }
}
